package com.zhudi.develop_demo_as.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.zhudi.develop_demo_as.BaseActivity;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.ui.fragment.order.OrderTypeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


public class OrderListActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private Context context;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.news_horizontalScrollView)
    HorizontalScrollView scrol;
    @Bind(R.id.news_fragment_viewpager)
    ViewPager viewPager;
    @Bind({R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5, R.id.tv6, R.id.tv7})
    List<TextView> tvTab;
    @Override
    protected int getLayoutView() {
        return R.layout.activity_order_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initView();
        setListener();
    }

    private void initView() {
        tvTitle.setText("团购订单");
        scrol.setOverScrollMode(View.OVER_SCROLL_NEVER);
        viewPager.setOverScrollMode(View.OVER_SCROLL_NEVER);

        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        //关闭预加载，默认一次只加载一个Fragment
        viewPager.setOffscreenPageLimit(1);
        viewPager.setCurrentItem(0);
        tvTab.get(0).setSelected(true);
        viewPager.addOnPageChangeListener(this);
    }

    private void setListener() {
        for (TextView view : tvTab){
            view.setOnClickListener(new tvTabClick());
        }
    }


    class tvTabClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < tvTab.size(); i++) {
                if (v.getId() == tvTab.get(i).getId()) {
                    viewPager.setCurrentItem(i);
                    resetAndSetTabs(i);
                }
            }
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        resetAndSetTabs(position);
        TextView tv = (TextView) findViewById(tvTab.get(position).getId());
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int left = tv.getLeft() - outMetrics.widthPixels / 2 + tv.getWidth() / 2;
        scrol.scrollTo(left, 0);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> arrayList;

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            arrayList = new ArrayList<Fragment>();
            for (int i = 0; i < tvTab.size(); i++) {
                arrayList.add(OrderTypeFragment.newInstance(String.valueOf(i)));
            }
        }

        @Override
        public Fragment getItem(int position) {
            return  arrayList.get(position);
        }

//        @Override
//        public Fragment getItem(int position) {
////            switch (position) {
////                case 0:
////                    return choicenessFragment;
////                case 1:
////                    NewsOtherSubFragmnet equipmentFragment = new NewsOtherSubFragmnet();
////                    equipmentFragment.setListPath(URLPath.EQUIPMENT_LISTVIEW_PATH);
////                    equipmentFragment.setPagerPath(URLPath.EQUIPMENT_VIEWPAGER_PATH);
////                    return equipmentFragment;
////                case 2:
////                    NewsOtherSubFragmnet imageFragment = new NewsOtherSubFragmnet();
////                    imageFragment.setListPath(URLPath.IMAGE_LISTVIEW_PATH);
////                    imageFragment.setPagerPath(URLPath.IMAGE_VIEWPAGER_PATH);
////                    return imageFragment;
////                case 3:
////                    NewsOtherSubFragmnet collegeFragment = new NewsOtherSubFragmnet();
////                    collegeFragment.setListPath(URLPath.COLLEGE_LISTVIEW_PATH);
////                    collegeFragment.setPagerPath(URLPath.COLLEGE_VIEWPAGER_PATH);
////                    return collegeFragment;
////                case 4:
////                    NewsOtherSubFragmnet travelFragment = new NewsOtherSubFragmnet();
////                    travelFragment.setListPath(URLPath.TRAVEL_LISTVIEW_PATH);
////                    travelFragment.setPagerPath(URLPath.TRAVEL_VIEWPAGER_PATH);
////                    return travelFragment;
////                case 5:
////                    NewsOtherSubFragmnet carFragment = new NewsOtherSubFragmnet();
////                    carFragment.setListPath(URLPath.CAR_LISTVIEW_PATH);
////                    carFragment.setPagerPath(URLPath.CAR_VIEWPAGER_PATH);
////                    return carFragment;
////                case 6:
////                    NewsOtherSubFragmnet phoneFragment = new NewsOtherSubFragmnet();
////                    phoneFragment.setListPath(URLPath.PHONE_LISTVIEW_PATH);
////                    phoneFragment.setPagerPath(URLPath.PHONE_VIEWPAGER_PATH);
////                    return phoneFragment;
////            }
////            return new OrderTypeFragment();
//            return arrayList.get(position);
//        }

        @Override
        public int getCount() {
            return tvTab.size();
        }
    }

    /**
     * 重置的Tab后设置Tab
     */
    private void resetAndSetTabs(int index) {
        for (int i = 0; i < tvTab.size(); i++) {
             tvTab.get(i).setSelected(false);
        }
        tvTab.get(index).setSelected(true);
    }
}
